package com.example.shaoying.yuejian.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.fragment.ClubeFragment;
import com.example.shaoying.yuejian.fragment.MainFragment;
import com.example.shaoying.yuejian.fragment.PersonFragment;
import com.example.shaoying.yuejian.fragment.PictureFragment;
import com.example.shaoying.yuejian.fragment.TrainFragment;


public class MainActivity extends FragmentActivity implements OnClickListener {


    private LinearLayout mTabpeixun;
    private LinearLayout mTabjulebu;
    private LinearLayout mTabdashi;
    private LinearLayout mTabshaitu;
    private LinearLayout mTabwode;

    private ImageButton mImgpeixun;
    private ImageButton mImgjulebu;
    private ImageButton mImgdashi;
    private ImageButton mImgshaitu;
    private ImageButton mImgwode;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        initView();
        initEvent();
        setSelect(0);
    }


    private void initEvent()
    {
        mTabpeixun.setOnClickListener( this);
        mTabjulebu.setOnClickListener(this);
        mTabdashi.setOnClickListener(this);
        mTabshaitu.setOnClickListener(this);
        mTabwode.setOnClickListener(this);
    }

    private void initView()
    {
        mTabpeixun = (LinearLayout) findViewById(R.id.peixun);
        mTabjulebu = (LinearLayout) findViewById(R.id.julebu);
        mTabdashi = (LinearLayout) findViewById(R.id.dashi);
        mTabshaitu = (LinearLayout) findViewById(R.id.shaitu);
        mTabwode = (LinearLayout) findViewById(R.id.wode);

        mImgpeixun = (ImageButton) findViewById(R.id.peixun_img);
        mImgjulebu = (ImageButton) findViewById(R.id.julebu_img);
        mImgdashi = (ImageButton) findViewById(R.id.dashi_img);
        mImgshaitu = (ImageButton) findViewById(R.id.shaitu_img);
        mImgwode = (ImageButton) findViewById(R.id.wode_img);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgpeixun.setImageResource(R.drawable.ic_peixun2);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubeFragment();transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgjulebu.setImageResource(R.drawable.ic_club2);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new MainFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgdashi.setImageResource(R.drawable.ic_dashi2);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new PictureFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgshaitu.setImageResource(R.drawable.ic_shaitu2);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new PersonFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgwode.setImageResource(R.drawable.ic_wode2);
                break;

            default:
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.peixun:
                setSelect(0);
                break;
            case R.id.julebu:
                setSelect(1);
                break;
            case R.id.dashi:
                setSelect(2);
                break;
            case R.id.shaitu:
                setSelect(3);
                break;
            case R.id.wode:
                setSelect(4);
                break;

            default:
                break;
        }
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }


    private void resetImgs()
    {
        mImgpeixun.setImageResource(R.drawable.ic_peixun1);
        mImgjulebu.setImageResource(R.drawable.ic_club1);
        mImgdashi.setImageResource(R.drawable.ic_dashi1);
        mImgshaitu.setImageResource(R.drawable.ic_shaitu1);
        mImgwode.setImageResource(R.drawable.ic_wode1);

    }
}



